import { onBeforeMount, onBeforeUnmount, onActivated, onDeactivated } from "vue";
import { useRoute } from "vue-router";

/**
 * eventListener
 */
export default function (key: string, fn: () => void) {
	const keepAlive = useRoute().meta.keepAlive;
	!keepAlive &&
		onBeforeMount(() => {
			window.addEventListener(key, fn);
		});
	!keepAlive &&
		onBeforeUnmount(() => {
			window.removeEventListener(key, fn);
		});
	keepAlive &&
		onActivated(() => {
			window.addEventListener(key, fn);
		});
	keepAlive &&
		onDeactivated(() => {
			window.removeEventListener(key, fn);
		});
}
